package top.fengleifeng.question;

import java.util.Objects;

/**
 * @program: leetcode-test
 * @description:
 * @author: fengleifeng
 * @create: 2020-09-22 19:55
 **/
public class Num337打家劫舍III {

    /**
     * Definition for a binary tree node.
     * public class TreeNode {
     * int val;
     * TreeNode left;
     * TreeNode right;
     * TreeNode(int x) { val = x; }
     * }
     */
    class Solution {
        public int rob(TreeNode root) {
            return choose(root);
        }
        private int choose(TreeNode now) {
            if (Objects.isNull(now)) {
                return 0;
            }
            int left = choose(now.left);
            int right = choose(now.right);
            int notNow = left + right;
            int leftLEFT = now.left == null ? 0 : choose(now.left.left);
            int leftRight = now.left == null ? 0 : choose(now.left.right);
            int rightLEFT = now.right == null ? 0 : choose(now.right.left);
            int rightRIGHT = now.right == null ? 0 : choose(now.right.right);
            int useNow = leftLEFT + leftRight + rightLEFT + rightRIGHT +now.val;
            return Math.max(notNow, useNow);
        }
    }
}
